package com.ruoyi.web.controller.productionre;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.production.domain.RegulatorReturn;
import com.ruoyi.production.domain.SimpleProduction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.FertilizerReturn;
import com.ruoyi.production.service.IFertilizerReturnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 化肥退回Controller
 *
 * @author ruoyi
 * @date 2021-07-19
 */
@Api("化肥退回")
@RestController
@RequestMapping("/production/fertilizerre")
public class FertilizerReturnController extends BaseController
{
    @Autowired
    private IFertilizerReturnService fertilizerReturnService;

    /**
     * 查询化肥退回列表
     */
    @ApiOperation("查询化肥退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:list')")
    @GetMapping("/list")
    public TableDataInfo list(FertilizerReturn fertilizerReturn)
    {
        startPage();
        List<FertilizerReturn> list = fertilizerReturnService.selectFertilizerReturnList(fertilizerReturn);
        return getDataTable(list);
    }

    /**
     * 导出化肥退回列表
     */
    @ApiOperation("导出化肥退回列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:export')")
    @Log(title = "化肥退回", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FertilizerReturn fertilizerReturn)
    {
        List<FertilizerReturn> list = fertilizerReturnService.selectFertilizerReturnList(fertilizerReturn);
        ExcelUtil<FertilizerReturn> util = new ExcelUtil<FertilizerReturn>(FertilizerReturn.class);
        return util.exportExcel(list, "化肥退回数据");
    }

    /**
     * 获取化肥退回详细信息
     */
    @ApiOperation("获取化肥退回详细信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:query')")
    @GetMapping(value = "/{fertilizerReturnId}")
    public AjaxResult getInfo(@PathVariable("fertilizerReturnId") Long fertilizerReturnId)
    {
        return AjaxResult.success(fertilizerReturnService.selectFertilizerReturnById(fertilizerReturnId));
    }

    /**
     * 新增化肥退回
     */
    @ApiOperation("新增化肥退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:add')")
    @Log(title = "化肥退回", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FertilizerReturn fertilizerReturn)
    {
        return toAjax(fertilizerReturnService.insertFertilizerReturn(fertilizerReturn));
    }

    /**
     * 修改化肥退回
     */
    @ApiOperation("修改化肥退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:edit')")
    @Log(title = "化肥退回", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FertilizerReturn fertilizerReturn)
    {
        return toAjax(fertilizerReturnService.updateFertilizerReturn(fertilizerReturn));
    }

    /**
     * 删除化肥退回
     */
    @ApiOperation("删除化肥退回")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:remove')")
    @Log(title = "化肥退回", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fertilizerReturnIds}")
    public AjaxResult remove(@PathVariable Long[] fertilizerReturnIds)
    {
        return toAjax(fertilizerReturnService.deleteFertilizerReturnByIds(fertilizerReturnIds));
    }

    /**
     * 获取化肥类型下拉框
     */
    @ApiOperation("获取化肥类型下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:type')")
    @GetMapping("/type")
    public TableDataInfo getType(){

        List<FertilizerReturn> list = fertilizerReturnService.getFertilizerType();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (FertilizerReturn fertilizerReturn:list) {
            Long id = fertilizerReturn.getFertilizerTypeId();
            String name = fertilizerReturn.getFertilizerType();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取化肥名称下拉框
     */
    @ApiOperation("获取化肥名称下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:name')")
    @GetMapping("/name")
    public TableDataInfo getName(){

        List<FertilizerReturn> list = fertilizerReturnService.getFertilizerName();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (FertilizerReturn fertilizerReturn:list) {
            Long id = fertilizerReturn.getFertilizerNameId();
            String name = fertilizerReturn.getFertilizerName();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取用户列表下拉框
     */
    @ApiOperation("获取用户列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:username')")
    @GetMapping("/username")
    public TableDataInfo getUserName(){

        List<FertilizerReturn> list = fertilizerReturnService.getUserName();
        List<SimpleProduction> simpleProductions = new ArrayList<>();
        for (FertilizerReturn fertilizerReturn:list) {
            Long id = fertilizerReturn.getUserId();
            String name = fertilizerReturn.getUserName();
            SimpleProduction simpleProduction = new SimpleProduction(id,name);
            simpleProductions.add(simpleProduction);
        }
        return getDataTable(simpleProductions);
    }

    /**
     * 获取NPK含量下拉框
     */
    @ApiOperation("获取NPK含量下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:content')")
    @GetMapping("/content/{fertilizerNameId}")
    public TableDataInfo getContent(@PathVariable("fertilizerNameId") Long fertilizerNameId){

        List<String> list = fertilizerReturnService.getNPKContent(fertilizerNameId);
        return getDataTable(list);
    }

    /**
     * 获取化肥品牌下拉框
     */
    @ApiOperation("获取化肥品牌下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:brand')")
    @GetMapping("/brand/{fertilizerNameId}")
    public TableDataInfo getBrand(@PathVariable("fertilizerNameId") Long fertilizerNameId){

        List<String> list = fertilizerReturnService.getBrand(fertilizerNameId);
        return getDataTable(list);
    }

    /**
     * 获取公司列表下拉框
     */
    @ApiOperation("获取公司列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('production:fertilizerre:origincompany')")
    @GetMapping("/origincompany/{fertilizerNameId}")
    public TableDataInfo getOriginCompany(@PathVariable("fertilizerNameId") Long fertilizerNameId){

        List<String> list = fertilizerReturnService.getOriginCompany(fertilizerNameId);
        return getDataTable(list);
    }
}
